
-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "LED_Clock"
VERSION = "1.0.0"

log.info("main", PROJECT, VERSION)

-- sys库是标配
_G.sys = require("sys")

if wdt then
    --添加硬狗防止程序卡死，在支持的设备上启用这个功能
    wdt.init(15000)--初始化watchdog设置为15s
    sys.timerLoopStart(wdt.feed, 10000)--10s喂一次狗
end

log.info("main", "Init wdt done")


--下面的GPIO引脚编号，请根据实际需要进行更改！

local LEDA = gpio.setup(pin.PB24, 0, gpio.PULLUP) -- PB24输出模式
local LEDB = gpio.setup(pin.PB25, 0, gpio.PULLUP) -- PB25输出模式
local LEDC = gpio.setup(pin.PB26, 0, gpio.PULLUP) -- PB26输出模式

log.info("main", "Init LED done")

log.info("main", "Set DS1302 PB9 mode")
local DS1302_DAT = gpio.setup(pin.PB9, 0, gpio.PULLUP) -- PB9输出模式
log.info("main", "Set DS1302 PB8 mode")
local DS1302_CLK = gpio.setup(pin.PB8, 0, gpio.PULLUP) -- PB8输出模式
log.info("main", "Set DS1302 PB10 mode")
local DS1302_CS = gpio.setup(pin.PB10, 0, gpio.PULLUP) -- PB10输出模式

log.info("main", "Set DS1302 pin mode done")

DS1302_DAT(0)
DS1302_CLK(0)
DS1302_CS(0)

log.info("main", "Init DS1302 done")

local DIN = gpio.setup(pin.PB00, 0, gpio.PULLUP) -- PB0输出模式
local CLK = gpio.setup(pin.PB04, 0, gpio.PULLUP) -- PB4输出模式
local STB_1 = gpio.setup(pin.PB01, 0, gpio.PULLUP) -- PB1输出模式
local STB_2 = gpio.setup(pin.PB03, 0, gpio.PULLUP) -- PB3输出模式

DIN(0);
CLK(0);
STB_1(0);
STB_2(0);


log.info("main", "Init LED screen done")

local curtime={}
curtime.year = 1970
curtime.month = 1
curtime.day = 1
curtime.hour = 0
curtime.min = 0
curtime.sec = 0
curtime.weekofday = 1

-- local DIN = D6
-- local CLK = D5
-- local STB_1 = D3;
-- local STB_2 = D4;
local WEEK_ENUM = {}

WEEK_ENUM.DayOfWeek_Monday = 1
WEEK_ENUM.DayOfWeek_Tuesday = 2
WEEK_ENUM.DayOfWeek_Wednesday = 3
WEEK_ENUM.DayOfWeek_Thursday = 4
WEEK_ENUM.DayOfWeek_Friday = 5
WEEK_ENUM.DayOfWeek_Saturday = 6
WEEK_ENUM.DayOfWeek_Sunday = 7


local f_dimming_1 = 0  --亮度可调  1-7级   0默认 最亮
local f_dimming_2 = 0  --亮度可调  1-7级   0默认 最亮

local seg_data_1 = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 } --缓存数组
local seg_data_2 = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 } --缓存数组

Font_Data = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F }  --数码管
Font_point = { 0x00, 0x03, 0x01, 0x02 }  --数码管 点   

table0 = { 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f } --LED从低位往高位移
table1 = { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe } --LED从高位往低位移
table2 = { 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 } --LED从1个亮到8个都点亮(从低位往高位)
table3 = { 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 } --LED从1个亮到8个都点亮(从高位往低位)
table4 = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff } --LED从8个全亮到一个都不亮(从低位往高位)
table5 = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff } --LED从8个全亮到一个都不亮(从高位往低位)
table6 = { 0xfe, 0xfc, 0xfa, 0xf6, 0xee, 0xde, 0xbe, 0x7e } --LED从低位往高位移(最低位始终为0)
table7 = { 0x7f, 0x3f, 0x5f, 0x6f, 0x77, 0x7b, 0x7d, 0x7e } --LED从高位往低位移(最高位始终为0)

HIGH = 1
LOW = 0

log.info("main", "Init variant done")

--取一个数的整数部分
function getIntPart(x)
    if x<= 0 then
        return math.ceil(x)
    end
    if math.ceil(x) == x then
        x = math.ceil(x)
    else
        x = math.ceil(x)-1
    end
    return x
end
function delay(x)
    --sys.wait(x)
end

--向DS1302 写一个字节的数据
function Write1302(dat)
    DS1302_CLK(0);            -- 拉低SCLK，为脉冲上升沿写入数据做好准备
    delay(1);       --稍微等待，使硬件做好准备
    for i=0, 7, 1 do      --连续写8个二进制位数据
	    DS1302_DAT(dat&0x01);    --取出dat的第0位数据写入1302
		delay(1);       --稍微等待，使硬件做好准备
		DS1302_CLK(1);           --上升沿写入数据
		delay(1);      --稍微等待，使硬件做好准备
		DS1302_CLK(0);           --重新拉低SCLK，形成脉冲
		dat = dat >> 1;          --将dat的各数据位右移1位，准备写入下一个数据位
    end
	
end
--向特定寄存器写一个字节的数据
function WriteSet1302(Cmd, dat)
    DS1302_CS(0);           --禁止数据传递
   	DS1302_CLK(0);          --确保写数居前SCLK被拉低
	DS1302_CS(1);           --启动数据传输
	delay(1);     --稍微等待，使硬件做好准备
	Write1302(Cmd);  --写入命令字
	Write1302(dat);  --写数据
	DS1302_CLK(1);          --将时钟电平置于已知状态
	DS1302_CS(0);           --禁止数据传递
end
--读一个字节的数据
function Read1302()
	dat = 0
    delay(1);       --稍微等待，使硬件做好准备
    DS1302_DAT = gpio.setup(pin.PB9, nil) -- PB9输入模式
	for i=0, 7, 1 do   --连续读8个二进制位数据
	    dat = dat >> 1;       --将dat的各数据位右移1位，因为先读出的是字节的最低位
	    if(DS1302_DAT()==1) then   --如果读出的数据是1
		    dat=dat |0x80;    --将1取出，写在dat的最高位
        end
		DS1302_CLK(1);       --将SCLK置于高电平，为下降沿读出
		delay(1);  --稍微等待
		DS1302_CLK(0);       --拉低SCLK，形成脉冲下降沿
		delay(1);  --稍微等待
    end
    DS1302_DAT = gpio.setup(pin.PB9, 0, gpio.PULLUP) -- PB9输出模式
    return dat;        --将读出的数据返回
end  
--读特定寄存器一个字节的数据
function  ReadSet1302(Cmd)
    DS1302_CS(0);                 --拉低RST
    DS1302_CLK(0);                --保写数居前SCLK被拉低
    DS1302_CS(1);                 --启动数据传输
  	Write1302(Cmd);       --写入命令字
  	dat=Read1302();       --读出数据
  	DS1302_CLK(1);              --将时钟电平置于已知状态
  	DS1302_CS(0);               --禁止数据传递
  	return dat;          --将读出的数据返回
end

function Init_DS1302()
    DS1302_CS(0);
    DS1302_CLK(0);
	WriteSet1302(0x8e,0x00);
	WriteSet1302(0x80,0x00);
end  


--HEX2BCD
function htob(i)
    return (i >> 4 ) * 10 + (i % 16)
end
--BCD2HEX
function btoh(i)
    return ((getIntPart(i / 10) << 4) + (i % 10));
end
--设置时间
function ds1302_set_time(void)
    WriteSet1302(0x8c, btoh(curtime.year-2000));
    WriteSet1302(0x88, btoh(curtime.month));
    WriteSet1302(0x86, btoh(curtime.day));
    WriteSet1302(0x84, btoh(curtime.hour));
    WriteSet1302(0x82, btoh(curtime.min));
    WriteSet1302(0x80, btoh(curtime.sec));
end

function WeekDay( y, m,  d)
    if (m == 1 or m == 2) then
        m = m + 12;
        y = y - 1;
    end
    A = getIntPart(d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7
    return A
end

--读取时间

--uchar code write_add[]={0x80,0x82,0x84,0x86,0x88,0x8c,0x8a};   //写地址
--uchar code read_add[] ={0x81,0x83,0x85,0x87,0x89,0x8d,0x8b};   //读地址
--uchar code init_ds[]  ={0x58,0x29,0x07,0x05,0x10,0x14,0x1};   

function ds1302_read_time()

    curtime.sec=htob(ReadSet1302(0x81));
    curtime.min=htob(ReadSet1302(0x83));
    curtime.hour=htob(ReadSet1302(0x85));
    curtime.day=htob(ReadSet1302(0x87));
    curtime.month=htob(ReadSet1302(0x89));
    curtime.year=htob(ReadSet1302(0x8d)) + 2000;
    --curtime.weekofday = htob(ReadSet1302(0x8b))
    curtime.weekofday = WeekDay(curtime.year,curtime.month,curtime.day)
    log.info("time",curtime.year,curtime.month,curtime.day,curtime.hour,curtime.min,curtime.sec,curtime.weekofday)
end    

function DIS1_SET_1()
    seg_data_1[11]=(seg_data_1[11]|0X01)
end

function DIS1_SET_2()
    seg_data_1[11]=(seg_data_1[11]|0X02)
end

function DIS1_SET_3()
    seg_data_1[8] =(seg_data_1[8] |0X04)
end

function DIS1_SET_4()
    seg_data_1[11]=(seg_data_1[11]|0X04)
end

function DIS1_SET_5()
    seg_data_1[11]=(seg_data_1[11]|0X08)
end

function DIS1_SET_6()
    seg_data_1[10] =(seg_data_1[10] |0X04)
end

function DIS1_SET_7()
    seg_data_1[1] =(seg_data_1[1] |0X80)
end

function DIS1_SET_8()
    seg_data_1[3] =(seg_data_1[3] |0X80)
end

function DIS1_SET_9()
    seg_data_1[5] =(seg_data_1[5] |0X80)
end

function DIS1_SET_10()
    seg_data_1[6] =(seg_data_1[6] |0X01)
end

function DIS1_SET_11()
    seg_data_1[12]=(seg_data_1[12]|0X04)
end

function DIS1_SET_12()
    seg_data_1[6] =(seg_data_1[6] |0X02)
end

function DIS1_SET_13()
    seg_data_1[11]=(seg_data_1[11]|0X10)
end

function DIS1_SET_14()
    seg_data_1[11]=(seg_data_1[11]|0X20)
end

function DIS1_SET_15()
    seg_data_1[11]=(seg_data_1[11]|0X40)
end

function DIS1_SET_16()
    seg_data_1[11]=(seg_data_1[11]|0X80)
end

function DIS1_SET_17()
    seg_data_1[12]=(seg_data_1[12]|0X01)
end

function DIS1_SET_18()
    seg_data_1[12]=(seg_data_1[12]|0X02)
end



--左右点
function DIS2_SET_1()
    seg_data_1[7]=(seg_data_1[7] |0X01)
end

function DIS2_SET_2()
    seg_data_1[7]=(seg_data_1[7] |0X02)
end

function DIS2_SET_3()
    seg_data_1[7]=(seg_data_1[7] |0X04)
end

function DIS2_SET_4()
    seg_data_1[7]=(seg_data_1[7] |0X08)
end

function DIS2_SET_5()
    seg_data_1[7]=(seg_data_1[7] |0X10)
end

function DIS2_SET_6()
    seg_data_1[7]=(seg_data_1[7] |0X20)
end

function DIS2_SET_7()
    seg_data_1[7]=(seg_data_1[7] |0X40)
end

function DIS2_SET_8()
    seg_data_1[7]=(seg_data_1[7] |0X80)
end

function DIS2_SET_9()
    seg_data_1[8]=(seg_data_1[8] |0X01)
end

function DIS2_SET_10()
    seg_data_1[8]=(seg_data_1[8] |0X02)
end
 
function DIS2_SET_11()
    seg_data_1[9]=(seg_data_1[9] |0X01)
end
 
function DIS2_SET_12()
    seg_data_1[9]=(seg_data_1[9] |0X02)
end
 
function DIS2_SET_13()
    seg_data_1[9]=(seg_data_1[9] |0X04)
end
 
function DIS2_SET_14()
    seg_data_1[9]=(seg_data_1[9] |0X08)
end
 
function DIS2_SET_15()
    seg_data_1[9]=(seg_data_1[9] |0X10) 
end

function DIS2_SET_16()
    seg_data_1[9]=(seg_data_1[9] |0X20) 
end

function DIS2_SET_17()
    seg_data_1[9]=(seg_data_1[9] |0X40) 
end

function DIS2_SET_18()
    seg_data_1[9]=(seg_data_1[9] |0X80) 
end

function DIS2_SET_19()
    seg_data_1[10]=(seg_data_1[10] |0X01) 
end

function DIS2_SET_20()
    seg_data_1[10]=(seg_data_1[10] |0X02) 
end


--置0  熄灭           
--中间杂点             
function DIS1_CLR_1()
    seg_data_1[11]=(seg_data_1[11]&(~0X01))
end

function DIS1_CLR_2()
    seg_data_1[11]=(seg_data_1[11]&(~0X02))
end

function DIS1_CLR_3()
    seg_data_1[8] =(seg_data_1[8] &(~0X04))
end

function DIS1_CLR_4()
    seg_data_1[11]=(seg_data_1[11]&(~0X04))
end

function DIS1_CLR_5()
    seg_data_1[11]=(seg_data_1[11]&(~0X08))
end

function DIS1_CLR_6()
    seg_data_1[10] =(seg_data_1[10] &(~0X04))
end

function DIS1_CLR_7()
    seg_data_1[1] =(seg_data_1[1] &(~0X80))
end

function DIS1_CLR_8()
    seg_data_1[3] =(seg_data_1[3] &(~0X80))
end

function DIS1_CLR_9()
    seg_data_1[5] =(seg_data_1[5] &(~0X80))
end

function DIS1_CLR_10()
    seg_data_1[6] =(seg_data_1[6] &(~0X01))
end

function DIS1_CLR_11()
    seg_data_1[12]=(seg_data_1[12]&(~0X04))
end

function DIS1_CLR_12()
    seg_data_1[6] =(seg_data_1[6] &(~0X02))
end

function DIS1_CLR_13()
    seg_data_1[11]=(seg_data_1[11]&(~0X10))
end

function DIS1_CLR_14()
    seg_data_1[11]=(seg_data_1[11]&(~0X20))
end

function DIS1_CLR_15()
    seg_data_1[11]=(seg_data_1[11]&(~0X40))
end

function DIS1_CLR_16()
    seg_data_1[12]=(seg_data_1[12]&(~0X80))
end

function DIS1_CLR_17()
    seg_data_1[12]=(seg_data_1[12]&(~0X01))
end

function DIS1_CLR_18()
    seg_data_1[12]=(seg_data_1[12]&(~0X02))
end



--左右点                                  
function DIS2_CLR_1()
    seg_data_1[7]=(seg_data_1[7] &(~0X01))
end

function DIS2_CLR_2()
    seg_data_1[7]=(seg_data_1[7] &(~0X02))
end

function DIS2_CLR_3()
    seg_data_1[7]=(seg_data_1[7] &(~0X04))
end

function DIS2_CLR_4()
    seg_data_1[7]=(seg_data_1[7] &(~0X08))
end

function DIS2_CLR_5()
    seg_data_1[7]=(seg_data_1[7] &(~0X10))
end

function DIS2_CLR_6()
    seg_data_1[7]=(seg_data_1[7] &(~0X20))
end

function DIS2_CLR_7()
    seg_data_1[7]=(seg_data_1[7] &(~0X40))
end

function DIS2_CLR_8()
    seg_data_1[7]=(seg_data_1[7] &(~0X80))
end

function DIS2_CLR_9()
    seg_data_1[8]=(seg_data_1[8] &(~0X01))
end

function DIS2_CLR_10()
    seg_data_1[8]=(seg_data_1[8] &(~0X02))  
end

function DIS2_CLR_11()
    seg_data_1[9]=(seg_data_1[9] &(~0X01))  
end

function DIS2_CLR_12()
    seg_data_1[9]=(seg_data_1[9] &(~0X02))  
end

function DIS2_CLR_13()
    seg_data_1[9]=(seg_data_1[9] &(~0X04))  
end

function DIS2_CLR_14()
    seg_data_1[9]=(seg_data_1[9] &(~0X08))  
end

function DIS2_CLR_15()
    seg_data_1[9]=(seg_data_1[9] &(~0X10))  
end

function DIS2_CLR_16()
    seg_data_1[9]=(seg_data_1[9] &(~0X20))  
end

function DIS2_CLR_17()
    seg_data_1[9]=(seg_data_1[9] &(~0X40))  
end

function DIS2_CLR_18()
    seg_data_1[9]=(seg_data_1[9] &(~0X80))  
end

function DIS2_CLR_19()
    seg_data_1[10]=(seg_data_1[10] &(~0X01))  
end

function DIS2_CLR_20()
    seg_data_1[10]=(seg_data_1[10] &(~0X02)) 
end



function digitalWrite(index, val)
    -- log.info("gpio", "In digitalWrite mark1",index,val)
    index(val);
    -- log.info("gpio", "In digitalWrite mark2")
end
function Make_Signal(index)

    i, j = 0 , 0;
    dis_data = 0;
    --log.info("gpio", "In Make_Signal mark1")
    digitalWrite(index, HIGH);
    digitalWrite(CLK, HIGH);
    --log.info("gpio", "In Make_Signal mark1",1)
    for i = 0, 7, 1 do                                --command 1 (#0000_0011B) 
        --STB_1=0;  
        digitalWrite(index, LOW);
        --CLK=0;
        digitalWrite(CLK, LOW);
        if (i == 1 or i == 0) then
            digitalWrite(DIN, HIGH);
        else 
            digitalWrite(DIN, LOW);
        end
        digitalWrite(CLK, HIGH);
    end
    --log.info("gpio", "In Make_Signal mark1",2)
    digitalWrite(index, HIGH);
    --log.info("gpio", "In Make_Signal mark1",3)
    for i = 0, 7, 1 do        --command 2 (#0100_0000B)
        digitalWrite(index, LOW);
        digitalWrite(CLK, LOW);
        if (i == 6)  then
            digitalWrite(DIN, HIGH);
        else
            digitalWrite(DIN, LOW);
        end
        digitalWrite(CLK, HIGH);
    end
    --log.info("gpio", "In Make_Signal mark1",4)
    digitalWrite(index, HIGH);
    --log.info("gpio", "In Make_Signal mark1",5)
    for i = 0, 7, 1 do        --command 3 (#1100_0000B)
        digitalWrite(index, LOW);
        digitalWrite(CLK, LOW);
        if (i == 6 or i == 7) then
            digitalWrite(DIN, HIGH);
        else 
            digitalWrite(DIN, LOW);
        end
        digitalWrite(CLK, HIGH);
    end
    --log.info("gpio", "In Make_Signal mark1",6)
    for i = 1, 14, 1 do
        if (index == STB_1) then
            --log.info("gpio", "In Make_Signal mark1",6,1)
            dis_data = seg_data_1[i];
            --log.info("gpio", "In Make_Signal mark1",6,2,dis_data,seg_data_1[i],i)
        else
            --log.info("gpio", "In Make_Signal mark1",6,3)
            dis_data = seg_data_2[i];
            --log.info("gpio", "In Make_Signal mark1",6,4,dis_data,seg_data_2[i],i)
        end
        for j = 0, 7, 1 do
            --log.info("gpio", "In Make_Signal mark1",6,5)
            digitalWrite(index, LOW);
            --log.info("gpio", "In Make_Signal mark1",6,6,CLK,LOW)
            digitalWrite(CLK, LOW);
            --log.info("gpio", "In Make_Signal mark1",6,7,DIN,dis_data)
            digitalWrite(DIN, dis_data & 0x01);
            --log.info("gpio", "In Make_Signal mark1",6,8)
            digitalWrite(CLK, HIGH);
            --log.info("gpio", "In Make_Signal mark1",6,9)
            dis_data = dis_data >> 1; --右移一位
            --log.info("gpio", "In Make_Signal mark1",6,10)
        end
    end
    --log.info("gpio", "In Make_Signal mark1",7)
    digitalWrite(index, HIGH);
    --log.info("gpio", "In Make_Signal mark2")
    for i = 0, 7, 1 do        --command 4 (#1000_0100B)
        digitalWrite(index, LOW);
        digitalWrite(CLK, LOW);
        if (f_dimming_1 == 0) then --显示亮度模式
            if (i == 3 or i == 7 or i == 2 or i == 1 or i == 0) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        elseif  (f_dimming_1 == 1) then
            if (i == 0 or i == 3 or i == 7) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        elseif  (f_dimming_1 == 2) then
            if (i == 1 or i == 3 or i == 7) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        elseif  (f_dimming_1 == 3) then
            if (i == 1 or i == 0 or i == 3 or i == 7) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        elseif  (f_dimming_1 == 4) then
            if (i == 2 or i == 3 or i == 7) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        elseif  (f_dimming_1 == 5) then
            if (i == 2 or i == 0 or i == 3 or i == 7) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        elseif  (f_dimming_1 == 6) then
            if (i == 2 or i == 1 or i == 3 or i == 7) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        elseif  (f_dimming_1 == 7) then 
            if (i == 2 or i == 1 or i == 0 or i == 7) then
                digitalWrite(DIN, HIGH);
            else
                digitalWrite(DIN, LOW);
            end
        end
        digitalWrite(CLK, HIGH);
    end
    --log.info("gpio", "In Make_Signal mark3")
    digitalWrite(index, HIGH);
    --log.info("gpio", "In Make_Signal mark4")
end

function Make_Signal_2()
  Make_Signal(STB_2);
end

function Make_Signal_1()
  Make_Signal(STB_1);
end


function delay_tim(delay_time_val)
  Make_Signal_2();
  sys.wait(getIntPart(delay_time_val/3));
  seg_data_2[3] = seg_data_2[1];
  seg_data_2[9] = seg_data_2[7];
  Make_Signal_2();
  sys.wait(getIntPart(delay_time_val/3));
  seg_data_2[5] = seg_data_2[1];
  seg_data_2[11] = seg_data_2[7];
  Make_Signal_2();
  sys.wait(getIntPart(delay_time_val/3));
end

function disp1()      --状态0 所有LED闪烁3次
  for i = 0, 2, 1 do
    seg_data_2[0] = 0x00; 
    seg_data_2[6] = 0x00;
    delay_tim(300);
    seg_data_2[0] = 0xff;
    seg_data_2[6] = 0xff;
    delay_tim(300);
  end
end

function disp0()      --状态9 间断8格的4个LED亮，然后逆时针逐渐点亮(直到全部点亮)
    seg_data_2[1] = 1
    seg_data_2[7] = 0
    Make_Signal_2();
    sys.wait(100)
    for i = 1, 7, 1 do
        seg_data_2[1] =  1 << (i)
        Make_Signal_2();
        sys.wait(100)
    end;
    seg_data_2[1] = 0
    for i = 1, 8, 1 do
        seg_data_2[7] = 1 << (i-1)
        Make_Signal_2();
        sys.wait(100)
    end;    
end

function display_clock(hour, min, DayOfWeek)
    for i=1, 14, 1 do
        seg_data_1[i]=0x00
    end

    seg_data_1[4] = Font_point[2];

    hour12 = hour;
    is_morning = true;
    if( hour > 12 ) then
        hour12 = hour % 12;
        is_morning = false;
    end
    --log.info("gpio", "clock", hour,hour12,min,DayOfWeek,Font_point[getIntPart(hour12 / 10) + 1])
    seg_data_1[2] = Font_point[getIntPart(hour12 / 10) + 1];   --时的十位
    seg_data_1[1] = Font_Data[hour12 % 10 + 1];    --时的个位
    seg_data_1[3] = Font_Data[getIntPart(min / 10) + 1];    --分的十位
    seg_data_1[5] = Font_Data[min % 10 + 1];    --分的个位
    if (is_morning) then --上午下午
        DIS1_SET_1();
        DIS1_SET_2();
        DIS1_SET_3();
    else
        DIS1_SET_4();
        DIS1_SET_5();
        DIS1_SET_6();
    end
    if(DayOfWeek == WEEK_ENUM.DayOfWeek_Monday) then
        DIS2_SET_1();
        DIS2_SET_11();
    elseif(DayOfWeek == WEEK_ENUM.DayOfWeek_Tuesday) then
        DIS2_SET_2();
        DIS2_SET_12();
    elseif(DayOfWeek == WEEK_ENUM.DayOfWeek_Wednesday) then
        DIS2_SET_3();
        DIS2_SET_13();
    elseif(DayOfWeek == WEEK_ENUM.DayOfWeek_Thursday) then
        DIS2_SET_4();
        DIS2_SET_14();
    elseif(DayOfWeek == WEEK_ENUM.DayOfWeek_Friday) then
        DIS2_SET_5();
        DIS2_SET_15();
    elseif(DayOfWeek == WEEK_ENUM.DayOfWeek_Saturday) then
        DIS2_SET_6();
        DIS2_SET_16();
    elseif(DayOfWeek == WEEK_ENUM.DayOfWeek_Sunday) then
        DIS2_SET_7();
        DIS2_SET_17();
    end

    Make_Signal(STB_1);
end

Make_Signal(STB_1);
Make_Signal(STB_2);

sys.taskInit(function()
    local count = 1
    local uid = ""
    if mcu then
        uid = mcu.unique_id():toHex()
    elseif nbiot then
        uid = nbiot.imei()
    end
    Init_DS1302()
    --[[
    ds1302_read_time()
    curtime.year = 2022
    curtime.month = 2
    curtime.day = 17
    curtime.hour = 8
    curtime.min = 24
    ds1302_set_time()
    ]]
    while 1 do
        -- 一闪一闪亮晶晶
        if( count > 3 ) then
            count = 1
        end
        LEDA(count == 0x01 and 1 or 0)
        LEDB(count == 0x02 and 1 or 0)
        LEDC(count == 0x03 and 1 or 0)
        -- log.info("gpio", "Go Go Go", uid, count)
        count = count + 1

        disp0();  --延迟一秒 
        ds1302_read_time()
        display_clock(curtime.hour,curtime.min,curtime.weekofday)
    end
end)


-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
